<?php
namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\shop\model\Artwork;
use app\shop\model\IndexTop as TopModel;
use app\shop\model\News as NewsModel;
use app\shop\model\Artist as ArtistModel;
use think\Hook;

/**
 * 推荐控制器
 */
class IndexTop extends Admin
{
    public function index($group = 1)
    {
    	//记录call_back
    	cookie('__forward__', $_SERVER['REQUEST_URI']);
        $data_list = TopModel::order('sort,id desc')->where('group',$group)->paginate();
        if($group == 1){
            $title = '首页艺术家推荐管理';

            $artist = ArtistModel::where('status',1)->column('id,name');
            return ZBuilder::make('table')
                ->setPageTitle($title)
//            ->setSearch(['id' => 'ID', 'title' => '头衔', 'name' => '姓名'])
                ->addColumns([
                    ['id', 'ID'],
                ['photo', '图片', 'picture'],
//                    ['url', '链接'],
                    ['artist_id', '艺术家',$artist],
                    ['create_time', '创建时间', 'datetime'],
                    ['status', '状态', 'switch'],
                    ['right_button', '操作', 'btn']
                ])
                ->addTopButton('add', [
                    'title' => '新增',
                    'icon'  => 'fa fa-plus-circle',
                    'href'  => url('add', ['group' => $group])
                ])
                ->addTopButtons('enable,disable,delete') // 批量添加顶部按钮
                ->addRightButtons('edit,delete') // 批量添加右侧按钮
                ->setRowList($data_list) // 设置表格数据
                ->fetch(); // 渲染页面
        }elseif($group == 2){
            $title = '首页话题推荐管理';

            $news = NewsModel::where('status',1)->column('id,title');
            return ZBuilder::make('table')
                ->setPageTitle($title)
//            ->setSearch(['id' => 'ID', 'title' => '头衔', 'name' => '姓名'])
                ->addColumns([
                    ['id', 'ID'],
                    ['photo', '图片', 'picture'],
//                    ['url', '链接'],
                    ['news_id', '话题',$news],
                    ['create_time', '创建时间', 'datetime'],
                    ['status', '状态', 'switch'],
                    ['right_button', '操作', 'btn']
                ])
                ->addTopButton('add', [
                    'title' => '新增',
                    'icon'  => 'fa fa-plus-circle',
                    'href'  => url('add', ['group' => $group])
                ])
                ->addTopButtons('enable,disable,delete') // 批量添加顶部按钮
                ->addRightButtons('edit,delete') // 批量添加右侧按钮
                ->setRowList($data_list) // 设置表格数据
                ->fetch(); // 渲染页面
        }elseif($group == 3){
            $title = '首页创作直播推荐管理';

            $news = NewsModel::where('status',1)->column('id,title');
            return ZBuilder::make('table')
                ->setPageTitle($title)
//            ->setSearch(['id' => 'ID', 'title' => '头衔', 'name' => '姓名'])
                ->addColumns([
                    ['id', 'ID'],
                    ['photo', '图片', 'picture'],
//                    ['url', '链接'],
                    ['news_id', '话题',$news],
                    ['create_time', '创建时间', 'datetime'],
                    ['status', '状态', 'switch'],
                    ['right_button', '操作', 'btn']
                ])
                ->addTopButton('add', [
                    'title' => '新增',
                    'icon'  => 'fa fa-plus-circle',
                    'href'  => url('add', ['group' => $group])
                ])
                ->addTopButtons('enable,disable,delete') // 批量添加顶部按钮
                ->addRightButtons('edit,delete') // 批量添加右侧按钮
                ->setRowList($data_list) // 设置表格数据
                ->fetch(); // 渲染页面
        }else{
            $title = '首页艺术品推荐管理';

            $news = Artwork::where('status',1)->column('id,title');
            return ZBuilder::make('table')
                ->setPageTitle($title)
//            ->setSearch(['id' => 'ID', 'title' => '头衔', 'name' => '姓名'])
                ->addColumns([
                    ['id', 'ID'],
                    ['photo', '图片', 'picture'],
//                    ['url', '链接'],
                    ['artwork_id', '话题',$news],
                    ['create_time', '创建时间', 'datetime'],
                    ['status', '状态', 'switch'],
                    ['right_button', '操作', 'btn']
                ])
                ->addTopButton('add', [
                    'title' => '新增',
                    'icon'  => 'fa fa-plus-circle',
                    'href'  => url('add', ['group' => $group])
                ])
                ->addTopButtons('enable,disable,delete') // 批量添加顶部按钮
                ->addRightButtons('edit,delete') // 批量添加右侧按钮
                ->setRowList($data_list) // 设置表格数据
                ->fetch(); // 渲染页面
        }
    }

    public function add($group = 1)
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            // 验证失败 输出错误信息

            if ($user = TopModel::create($data)) {
                Hook::listen('top_add', $user);
                // 记录行为
                action_log('banner_user_add', 'artist_user_add', $user['id'], UID);
                $this->success('新增成功', url('index',array('group'=>$data['group'])));
            } else {
                $this->error('新增失败');
            }
        }
        if($group ==1){//人物推荐
            $artist = ArtistModel::where('status',1)->column('id,name');
            return ZBuilder::make('form')
                ->setPageTitle('新增艺术家推荐')
                ->addFormItems([
                    ['select', 'artist_id', '艺术家', '', $artist],
                    ['image', 'photo', '图片'],
                    ['hidden', 'group', $group],
                    ['text', 'sort', '排序', '', 100],
                    ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
                ])
                ->fetch();
        }
        if($group ==2){//话题推荐
            $artist = NewsModel::where('status',1)->column('id,title');
            return ZBuilder::make('form')
                ->setPageTitle('新增话题推荐')
                ->addFormItems([
                    ['select', 'news_id', '话题', '', $artist],
                    ['image', 'photo', '图片'],
                    ['hidden', 'group', $group],
                    ['text', 'sort', '排序', '', 100],
                    ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
                ])
                ->fetch();
        }
        if($group == 4){//话题推荐
            $artist = Artwork::where('status',1)->column('id,title');
            return ZBuilder::make('form')
                ->setPageTitle('新增艺术品推荐')
                ->addFormItems([
                    ['select', 'artwork_id', '艺术品', '', $artist],
                    ['image', 'photo', '图片'],
                    ['hidden', 'group', $group],
                    ['text', 'sort', '排序', '', 100],
                    ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
                ])
                ->fetch();
        }
    }

    public function edit($id = null)
    {
        if ($id === null) $this->error('缺少参数');

        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $artist = ArtistModel::where('status',1)->column('id,name');
            if (TopModel::update($data)) {
                $user = TopModel::get($data['id']);
                Hook::listen('banner_user_edit', $user);
                $this->success('编辑成功', cookie('__forward__'));
            } else {
                $this->error('编辑失败');
            }
        }

        // 获取数据
        $info = TopModel::where('id', $id)->find();
//        echo $info->group;exit;
        if($info->group == 1){
            $artist = ArtistModel::where('status',1)->column('id,name');
            // 使用ZBuilder快速创建表单
            return ZBuilder::make('form')
                ->setPageTitle('编辑艺术家推荐')
                ->addFormItems([
//                    ['text', 'title', '标题', '必填'],

                    ['select', 'artist_id', '艺术家', '', $artist],
                    ['image', 'photo', '图片'],
                    ['hidden', 'id'],
                    ['hidden', 'group', $info->group],
                    ['text', 'sort', '排序', '', 100],
                    ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
                ])
                ->setFormData($info) // 设置表单数据
                ->fetch();
        }
        if($info->group == 2){
            $artist = NewsModel::where('status',1)->column('id,title');
            // 使用ZBuilder快速创建表单
            return ZBuilder::make('form')
                ->setPageTitle('编辑话题推荐')
                ->addFormItems([
//                    ['text', 'title', '标题', '必填'],

                    ['select', 'news_id', '话题', '', $artist],
                    ['image', 'photo', '图片'],
                    ['hidden', 'id'],
                    ['hidden', 'group', $info->group],
                    ['text', 'sort', '排序', '', 100],
                    ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
                ])
                ->setFormData($info) // 设置表单数据
                ->fetch();
        }
        if($info->group == 4){
            $artist = Artwork::where('status',1)->column('id,title');
            // 使用ZBuilder快速创建表单
            return ZBuilder::make('form')
                ->setPageTitle('编辑艺术品推荐')
                ->addFormItems([
//                    ['text', 'title', '标题', '必填'],

                    ['select', 'artwork_id', '艺术品', '', $artist],
                    ['image', 'photo', '图片'],
                    ['hidden', 'id'],
                    ['hidden', 'group', $info->group],
                    ['text', 'sort', '排序', '', 100],
                    ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
                ])
                ->setFormData($info) // 设置表单数据
                ->fetch();
        }
    }

    public function delete($ids = [])
    {
        Hook::listen('artist_user_delete', $ids);
        return $this->setStatus('delete');
    }


    /**
     * 设置用户状态：删除、禁用、启用
     */
    public function setStatus($type = '', $record = [])
    {
        $ids        = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        $uid_delete = is_array($ids) ? '' : $ids;
        $ids        = array_map('get_nickname', (array)$ids);
        return parent::setStatus($type, ['user_'.$type, 'shop_artist', $uid_delete, UID, implode('、', $ids)]);
    }
}

